package com.freshmarket.freshmarket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freshmarket.freshmarket.controller.form.InsertProductForm;
import com.freshmarket.freshmarket.controller.form.SelectProductForm;
import com.freshmarket.freshmarket.controller.result.BackProductResult;
import com.freshmarket.freshmarket.dao.ProductBatchMapper;
import com.freshmarket.freshmarket.dao.ProductBatchOperationMapper;
import com.freshmarket.freshmarket.dao.ProductMapper;
import com.freshmarket.freshmarket.entity.Product;
import com.freshmarket.freshmarket.entity.ProductBatch;
import com.freshmarket.freshmarket.entity.ProductBatchOperation;
import com.freshmarket.freshmarket.exception.ArgsMistakeException;
import com.freshmarket.freshmarket.exception.RepetitiveProductException;
import com.freshmarket.freshmarket.service.IProductService;
import com.freshmarket.freshmarket.utils.Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-04
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    final ProductMapper productMapper;
    final ProductBatchMapper productBatchMapper;
    final ProductBatchOperationMapper productBatchOperationMapper;

    public ProductServiceImpl(ProductMapper productMapper, ProductBatchMapper productBatchMapper, ProductBatchOperationMapper productBatchOperationMapper) {
        this.productMapper = productMapper;
        this.productBatchMapper = productBatchMapper;
        this.productBatchOperationMapper = productBatchOperationMapper;
    }

    @Transactional
    public void insertProduct(InsertProductForm insertProductForm) throws Exception {
        int productId = -1;

        List<Product> product_name = productMapper.selectList(new QueryWrapper<Product>().eq("product_name", insertProductForm.getProductName()));
        if (product_name.size() > 0) {
            throw new RepetitiveProductException();
        } else {
            Product product = new Product();
            product.setProductExpireTime(insertProductForm.getProductExpireTime());
            product.setProductImgUrl(insertProductForm.getProductImg());
            product.setProductName(insertProductForm.getProductName());
            product.setProductNum(insertProductForm.getProductNum());
            product.setProductPrice(insertProductForm.getProductPrice());
            product.setProductTypeId(insertProductForm.getProductTypeId());
            product.setProductStatus("已上架");
            productMapper.insert(product);
            productId = product.getProductId();
        }

        ProductBatch productBatch = new ProductBatch();
        productBatch.setProductBatchSource(insertProductForm.getSource());
        productBatch.setProductBatchNum(insertProductForm.getProductNum());
        productBatch.setProductId(productId);
        productBatchMapper.insert(productBatch);

        ProductBatchOperation productBatchOperation = new ProductBatchOperation();
        productBatchOperation.setProductBatchId(productBatch.getProductBatchId());
        productBatchOperation.setProductBatchOperationStatus("上架");
        productBatchOperation.setProductBatchOperationTime(Utils.getDateTime());
        productBatchOperationMapper.insert(productBatchOperation);
    }

    /**
     * @author Squbi
     * @date 2020/9/6 19:12
     * @Description 查询
     */
    public List<BackProductResult> selectProduct(SelectProductForm selectProductForm) {
        if (selectProductForm.getProductStatus() != null && !selectProductForm.getProductStatus().equals("已上架")
                && !selectProductForm.getProductStatus().equals("已下架") &&! selectProductForm.getProductStatus().equals("")) {
            throw new ArgsMistakeException("传入的参数错误:" + selectProductForm.getProductStatus());
        }
        selectProductForm.setPageIndex((selectProductForm.getPageIndex()-1)*5);
        return productMapper.selectProduct(selectProductForm);
    }


}
